package io.renren.controller;

import com.mongodb.client.gridfs.model.GridFSFile;
import io.renren.common.utils.FileUtil;
import io.renren.common.utils.MongodbUtils;
import io.renren.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.bson.Document;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 文档管理 上传 下载
 */
@RestController
@RequestMapping("/file")
@Api(tags = "文档上传接口")
public class UploadApi {

    /**
     * 上传
     */
    @PostMapping(value = "/upload")
    //@PostMapping(value = "/upload", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    //@ApiParam(value = "上传的文件", required = true) MultipartFile multipartFile
    @ApiOperation("文件上传")
    private void upload(
            //@RequestParam(value="file",required=false) MultipartFile multipartFile
            @ApiParam(value = "上传的文件", required = true) MultipartFile multipartFile
    ) throws Exception {
        Document metadata = new Document();
        GridFSFile gridFSFile = MongodbUtils.saveFile(multipartFile, metadata);
        gridFSFile.getMetadata().get("_contentType");
    }

    /**
     * 查看文件头
     */
    @PostMapping(value = "/getFileHeader")
    //@PostMapping(value = "/upload", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    //@ApiParam(value = "上传的文件", required = true) MultipartFile multipartFile
    @ApiOperation("查看文件头")
    private R getFileHeader(
            //@RequestParam(value="file",required=false) MultipartFile multipartFile
            @ApiParam(value = "上传的文件", required = true) MultipartFile multipartFile
    ) {
        return R.ok().put("head", FileUtil.getFileHeader(multipartFile));
    }

    /**
     * 下载
     */
    @GetMapping(value = "/downFile")
    @ApiOperation("下载")
    private void downFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //MongodbUtils.downFile(request, response);
    }

    /**
     * 刪除
     */
    @GetMapping(value = "/deleteFile")
    @ApiOperation("刪除")
    private void deleteFile(HttpServletRequest request) throws Exception {
        //MongodbUtils.deleteFile(request);
    }

    /**
     * 刪除
     */
    @GetMapping(value = "/showImage")
    @ApiOperation("showImage")
    private void test(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //MongodbUtils.showImage(request, response);
    }
}
